<template>
    <div>
        <el-container>
            <el-main>
                <div class="report">
                    <p>
                        咨询列表<el-button type="primary" size="large" @click="addTableVisible = true">新增</el-button>
                    </p>
                </div>
                <div clss="report-info-body">
                    <div>
                        <el-table fixed :data="consultForm" stripe style="width: 100%" max-height="500">
                            <el-table-column prop="tea_name" label="咨询师" align="center"></el-table-column>
                            <el-table-column prop="stu_name" label="学生姓名" align="center"></el-table-column>
                            <el-table-column prop="week_name" label="工作日" align="center"></el-table-column>
                            <el-table-column prop="detail_time" label="时间段" align="center"></el-table-column>
                            <el-table-column prop="consultCount" label="咨询次数" align="center"></el-table-column>
                            <el-table-column prop="isEnd" label="是否结案" :formatter="isEndback" align="center"></el-table-column>
                            <el-table-column align="center" label="操作" fixed="right" width="200">
                              <template #default="scope">
                                <el-button size="mini" type="primary" @click="EditForm(scope.$index, scope.row)"
                                           style="background-color: #409EFF;border-color: #409EFF;color:#fff;">编辑</el-button>
                                <el-button size="mini" type="primary" @click="handleDelete(scope.$index, scope.row)"
                                           style="background-color: #409EFF;border-color: #409EFF;color:#fff;">删除</el-button>
                              </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </div>
                <div>
                    <el-dialog title="咨询列表" v-model="EditTableVisible" :before-close="handleClose">
                        <el-form :model="Editconsult" ref="editconsultForm" :rules="addRules">
                            <el-form-item label="咨询师" prop="tea_name" style="width: 80%;">
                                <el-select v-model="Editconsult.tea_name" >
                                    <el-option
                                            v-for="item in Consultants"
                                            :label="item.stuName"
                                            :value="item.stuName"
                                            :key="item.stuName"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="学生姓名" prop="stu_name" style="width: 80%;">
                                <el-select v-model="Editconsult.stu_name" disabled >
                                    <el-option
                                            v-for="item in Stus"
                                            :label="item.stuName"
                                            :value="item.stuName"
                                            :key="item.stuName"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="工作日" prop="week_name" style="width: 80%;">
                                <el-select name="time" v-model="Editconsult.week_name" @change="getDict_Time">
                                    <el-option
                                            v-for="item in timeDict_Week"
                                            :label="item"
                                            :value="item"
                                            :key="item"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="时间段" prop="detail_time" style="width: 80%;">
                                <el-select v-model="Editconsult.detail_time">
                                    <el-option
                                            v-for="item in timeDict_Time"
                                            :label="item"
                                            :value="item"
                                            :key="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="是否结案" prop="isEnd" style="width: 80%;">
                                <el-select v-model="Editconsult.isEnd">
                                    <el-option
                                            v-for="item in isEndform"
                                            :label="item.label"
                                            :value="item.value"
                                            :key="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                        <template #footer>
                            <el-button @click="EditTableVisible = false">取消</el-button>
                            <el-button type="primary" @click="EditSubmit">提交</el-button>
                        </template>
                    </el-dialog>
                </div>
                <div>
                    <el-dialog title="咨询列表" v-model="addTableVisible" :before-close="handleClose">
                        <el-form :model="formData" ref="addconsultForm" :rules="addRules">
                            <el-form-item label="咨询师" prop="tea_name" style="width: 80%;">
                                <el-select v-model="formData.tea_name" >
                                    <el-option
                                        v-for="item in Consultants"
                                        :label="item.stuName"
                                        :value="item.stuName"
                                        :key="item.stuName"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="学生姓名" prop="stu_name" style="width: 80%;">
                                <el-select v-model="formData.stu_name" >
                                    <el-option
                                            v-for="item in Stus"
                                            :label="item.stuName"
                                            :value="item.stuName"
                                            :key="item.stuName"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="工作日" prop="week_name" style="width: 80%;">
                                <el-select name="time" v-model="formData.week_name" @change="getDict_Time">
                                    <el-option
                                            v-for="item in timeDict_Week"
                                            :label="item"
                                            :value="item"
                                            :key="item"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="时间段" prop="detail_time" style="width: 80%;">
                                <el-select v-model="formData.detail_time">
                                    <el-option
                                            v-for="item in timeDict_Time"
                                            :label="item"
                                            :value="item"
                                            :key="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="是否结案" prop="isEnd" style="width: 80%;">
                                <el-select v-model="formData.isEnd">
                                    <el-option
                                            v-for="item in isEndform"
                                            :label="item.label"
                                            :value="item.value"
                                            :key="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                        <template #footer>
                            <el-form ref="addconsultForm" :rules="addRules">
                            <el-button @click="addTableVisible = false">取消</el-button>
                            <el-button type="primary" @click="handleSubmit">提交</el-button>
                            </el-form>
                        </template>
                    </el-dialog>
                </div>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    import request from "../../utils/request";
    import {ElMessage} from "element-plus";
    import {getWeekday} from "../../api/time";
    import {getSingleTime} from "../../api/time";
    export default {
        name: "consultPage",
        data(){
            return{
                addTableVisible : false,
                EditTableVisible : false,
                isEndform:[{
                    value:'0',
                    label:'否',
                },{
                    value:'1',
                    label:'是'
                }],
                timeDict_Week:[],
                timeDict_Time:[],
                Consultants: [],
                Stus:[],
                Times:[],
                consultForm:[{
                    consultCount:'',
                    detail_time:'',
                    id:'',
                    isEnd:'',
                    stuId:'',
                    stu_name:'',
                    teaId:'',
                    tea_name:'',
                    timeId:'',
                    week_name:'',
                }],
                Editconsult:{
                    id:'',
                    stu_id:'',
                    tea_name:'',
                    week_name:'',
                    detail_time:'',
                    teaId:'',
                    stu_name:'',
                    timeId:'',
                    isEnd:'',
                },
                formData:{
                    id:'',
                    tea_name:'',
                    week_name:'',
                    detail_time:'',
                    teaId:'',
                    stu_name:'',
                    isEnd:'',
                    timeId:'',
                },

                addRules: {
                    tea_name:[{
                        required : true,
                        message:'不能为空',
                        trigger:'change',
                    }],
                    stu_name:[{
                        required : true,
                        message:'不能为空',
                        trigger:'blur'
                    }],
                    week_name:[{
                        required : true,
                        message:'不能为空',
                        trigger:'change'
                    }],
                    detail_time:[{
                        required : true,
                        message:'不能为空',
                        trigger:'change'
                    }],
                    consultCount:[{
                        required : true,
                        message:'不能为空',
                        trigger:'blur'
                    }],
                    isEnd:[{
                        required : true,
                        message:'不能为空',
                        trigger:'change'
                    }],
                }
            }
        },

        methods :{

            getTimeid(){
                return request({
                    url:'/api/singleTime',
                    method:'get'
                })
            },


            get_Time(){
                this.getTimeid().then(res=>{
                    if(res.code ===1 ){
                        console.log(res)
                        this.Times = res.data
                    }
                })
            },


            getTimeByWeekday(query){
                return request({
                    url: '/api/singleTime/dict/time/'+query,
                    method: 'get'
                })
            },

            getDict_Time(e){
                this.getTimeByWeekday(e).then(res=>{
                    if(res.code === 1){
                        console.log(res)
                        this.timeDict_Time = res.data
                    }
                })
            },
            getSingleTimeDict_Week(){
                getWeekday().then(res=>{
                    if(res.code === 1){
                        console.log(res)
                        this.timeDict_Week = res.data
                    }
                })
            },


            getstu(){
                return request({
                    url:'/api/user',
                    method:'get'
                })
            },

            get_stu(){
              this.getstu().then(res=>{
                  if(res.code===1){
                      console.log(res)
                      this.Stus=res.data
                  }
              })
            },

            //咨询师
            getConsultants(){
                return request({
                    url: '/api/user/consultants',
                    method: 'get'
                })
            },

            get_Consultants(){
                this.getConsultants().then(res=>{
                    if(res.code === 1){
                        console.log(res)
                        this.Consultants = res.data
                    }
                })
            },


            //
            isEndback(row, column) {
                if (row.isEnd === '1' || row.isEnd === 1 ){
                    return '是'
                }else if(row.isEnd === '0') {
                    return '否'
                }
            },

            //增添接口
            submitConsult(query){
                return request({
                    url:'/api/consultMerge/addConsult/',
                    method:'post',
                    data: query,
                })
            },

            //编辑接口
            editConsult(query){
                return request({
                    url:'/api/consultMerge/a/updateconsultMerge/',
                    method:'put',
                    data: query,
                })
            },

            //编辑提交
            async EditSubmit(){
                this.EditTableVisible =false
                await console.log(this.Editconsult)
                this.Consultants.forEach(x =>{
                    console.log(x)
                    if(this.Editconsult.tea_name===x.stuName){
                        this.Editconsult.teaId = x.id
                    }
                })
                this.Stus.forEach(x =>{
                    console.log(x)
                    if(this.Editconsult.stu_name===x.stuName){
                        this.Editconsult.stuId = x.id
                    }
                })
                this.Times.forEach(x => {
                    console.log(x)
                    if(this.Editconsult.detail_time === x.timeDay && this.Editconsult.week_name === x.weakDay){
                        this.Editconsult.timeId = x.id
                    }
                })
                this.editConsult(this.Editconsult).then(res=>{
                    if(res.code === 1 ) {
                        console.log(res)
                        this.getData()
                        ElMessage.success(res.message)
                    }else{
                        ElMessage.error(res.message);
                    }
                })
                console.log('提交')
            },


            //添加提交
            async handleSubmit(){
                this.addTableVisible =false
                await console.log(this.formData)
                this.Consultants.forEach(x =>{
                    console.log(x)
                    if(this.formData.tea_name===x.stuName){
                        this.formData.teaId = x.id
                    }
                })
                this.Stus.forEach(x =>{
                    console.log(x)
                    if(this.formData.stu_name===x.stuName){
                        this.formData.stuId = x.id
                    }
                })
                this.Times.forEach(x => {
                    console.log(x)
                    if(this.formData.detail_time === x.timeDay && this.formData.week_name === x.weakDay){
                        this.formData.timeId = x.id
                    }
                })
                this.submitConsult(this.formData).then(res=>{
                    if(res.code === 1 ) {
                        console.log(res)
                        this.getData()
                        ElMessage.success(res.data)
                    }else{
                        ElMessage.error(res.data);
                    }
                })
                console.log('提交')
            },
            //删除接口
            deleteById(query){
                return request({
                    url: '/api/consultMerge/deleteById/'+query,
                    method: 'delete'
                })
            },

            //删除
            handleDelete(index,row){
                console.log(row.id)
                this.deleteById(row.id).then(res=>{
                    if(res.code === 1){
                        console.log(res)
                        ElMessage.success(res.data)
                        this.getData()
                    } else {
                        ElMessage.error(res.data)
                    }
                })
                this.getData()
            },


            //编辑
            EditForm(index,row) {
                console.log(row)
                this.EditTableVisible = true;
                this.Editconsult = Object.assign({},row);
            },


            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            //获取接口
            getconsultPage(){
                return request({
                    url:'/api/consultMerge/getall',
                    method:'get'
                })
            },
            //获取数据
             getData(){
                this.getconsultPage().then(res => {
                    if(res.code ===1) {
                            // this.consultForm[i].stu_name = res.data[i].stu_name;
                            this.consultForm = res.data
                            console.log(this.consultForm)
                    }
                })
            },
        },
        created() {
            this.getData()
            this.get_Consultants()
            this.getDict_Time()
            this. getSingleTimeDict_Week()
            this.get_stu()
            this.get_Time()
        }
    }
</script>

<style scoped>
    .report {
        width: auto;
        height: 56px;
        background-color: #EDEFF2;
        font-size: 22px;
        /* font-family: SimHei; */
        font-weight: 400;
        color: #333333;
        line-height: 48px;
        border-left: 6px solid #409EFF;
        text-align: left;
    }
</style>